<?php
class Api_Lib_Rsp_Sample_Kucun {

    function __construct() {
        $this->_modelExample = FLEA::getSingleton('Model_Sample_Kucun');
    }

    /**
     * getlist 获取库存列表
     * Time：2019/11/11 15:11:10
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getList($params,& $service){
        $pageNum     = $params['pageNum'] ? $params['pageNum'] : 20;
        $currentPage = $params['page'] ? $params['page'] : 1;

        $sql = "SELECT sum(cnt) as cnt,productId,type,proDesc,kuweiId,code,gexing,zuzhi,tanli,hzlId from sample_kucun
              where 1 ";
        if($params['code']!=''){
            $sql.=" and code like '%{$params['code']}%'";
        }
        if($params['key']!=''){
            $sql.=" and proDesc like '%{$params['key']}%'";
        }
        if($params['proDesc']!='') {
            $sql.=" and proDesc like '%{$params['proDesc']}%'";
        }
        if($params['type']) {
            $sql.=" and type = '{$params['type']}'";
        }
        if($params['productId']) {
            $sql.=" and productId = '{$params['productId']}'";
        }
        if($params['kuweiId']) {
            $sql.=" and kuweiId = '{$params['kuweiId']}'";
        }

        //是否显示无库存的数据
        $having = $params['hasKucun'] ? " having sum(cnt)>0 " : '';
        $sql .=" group by productId,type,proDesc,kuweiId,code
        {$having}
        order by productId,code,kuweiId";

        // dump($sql);exit;
        FLEA::loadClass('TMIS_Pager');
        $pager = new TMIS_Pager($sql,null,null,$pageNum ,($currentPage - 1));
        $rowset = $pager->findAll();

        $modelProduct = FLEA::getSingleton('Model_Jichu_Product');
        $modelKuwei = FLEA::getSingleton('Model_Jichu_Kuwei');
        // dump($rowset);exit;
        foreach ($rowset as $key => & $v) {
            //产品明细
            $sql = "select x.*,y.kindName from jichu_product x
                left join jichu_prokind y on x.kindId=y.id
              where x.id = '{$v['productId']}'";
            $tmp = $this->_modelExample->findBySql($sql);
            $v['proCode']  = $tmp[0]['proCode'];
            $v['proName']  = $tmp[0]['proName'];
            $v['kindName'] = $tmp[0]['kindName'];
            //特殊处理下产品数据
            $_tRow = array();
            $modelProduct->filedValue3($_tRow ,$tmp[0] ,$filedsSingle);
            // $v['otherDesc'] = $_tRow;
            foreach ($_tRow as $tk => & $desc) {
                $v['otherDesc'][] = $tk.':'.$desc;
                $v['others'][] = array('title'=>$tk  ,'value'=>$desc);
            }
            $v['othersRows'] = ceil(count($v['others'])/2);//用户计算显示其他信息的行数
            $v['otherDesc'] = join(' ',$v['otherDesc']);
            // $v['otherDesc'] = $modelProduct->filedOtherFormat($tmp[0] ,'text','' ,$filedsDefault);

            $v['cnt'] = round($v['cnt'],6);

            $v['kuweiName'] = '';
            if($v['kuweiId']){
                $_tmpKw = $modelKuwei->find($v['kuweiId']);
                $v['kuweiName'] = $_tmpKw['kuweiName'].'';
            }

            // 后整理
            if($v['hzlId']){
                $sql = "SELECT * from jichu_gongyi where id in({$v['hzlId']})";
                $tmp = $this->_modelExample->findBySql($sql);
                $v['hzl'] = join('，',array_col_values($tmp,'gongyiName'));
            }

        }

        return array('row'=>$rowset,'page'=>$currentPage,'total'=>$pager->totalCount,'pageCount'=>count($rowset));
    }


    /**
     * 被采样统计
     * Time：2019/11/20 15:46:46
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getSampleReportList($params,& $service){
        // error_reporting(E_ALL);
        $pageNum     = $params['pageNum'] ? $params['pageNum'] : 20;
        $currentPage = $params['page'] ? $params['page'] : 1;

        $sql = "SELECT x.productId,x.proDesc,sum(x.cnt) as cycnt,x.type,x.code,count(x.chukuId) as num
                from sample_chuku2product x
                left join sample_chuku y on x.chukuId=y.id
                where 1";
        if($params['productId']){
            $sql .= " and x.productId = '{$params['productId']}'";
        }
        if($params['proDesc']){
            $sql .= " and x.proDesc like '%{$params['proDesc']}%'";
        }
        if($params['code']){
            $sql .= " and x.code like '%{$params['code']}%'";
        }
        if($params['dateFrom']){
            $sql .= " and y.chukuDate >= '{$params['dateFrom']}' and y.chukuDate <= '{$params['dateTo']}'";
        }
        if($params['type']) {
            $sql.=" and x.type = '{$params['type']}'";
        }
        $sql .= " group by x.productId,x.type,x.proDesc,x.code order by cycnt desc,num desc";

        // dump($sql);exit;
        FLEA::loadClass('TMIS_Pager');
        $pager = new TMIS_Pager($sql,null,null,$pageNum ,($currentPage - 1));
        $rowset = $pager->findAll();
        // dump($rowset);exit;
        $modelProduct = FLEA::getSingleton('Model_Jichu_Product');
        foreach ($rowset as $key => & $v) {
            //产品明细
            $sql = "select x.*,y.kindName from jichu_product x
                left join jichu_prokind y on x.kindId=y.id
              where x.id = '{$v['productId']}'";
            $tmp = $this->_modelExample->findBySql($sql);
            $v['proCode']  = $tmp[0]['proCode'];
            $v['proName']  = $tmp[0]['proName'];
            $v['kindName'] = $tmp[0]['kindName'];
            //特殊处理下产品数据
            $_tRow = array();
            $modelProduct->filedValue3($_tRow ,$tmp[0] ,$filedsSingle);
            // $v['otherDesc'] = $_tRow;
            foreach ($_tRow as $tk => & $desc) {
                $v['otherDesc'][] = $tk.':'.$desc;
                $v['others'][] = array('title'=>$tk  ,'value'=>$desc);
            }
            $v['othersRows'] = ceil(count($v['others'])/2);//用户计算显示其他信息的行数
            $v['otherDesc'] = join(' ',$v['otherDesc']);
            // $v['otherDesc'] = $modelProduct->filedOtherFormat($tmp[0] ,'text','' ,$filedsDefault);

            //其他数据
            $v['cycnt'] = round($v['cycnt'],6);
        }

        return array('row'=>$rowset,'page'=>$currentPage,'total'=>$pager->totalCount,'pageCount'=>count($rowset));
    }


    /**
     * 被采样人统计
     * Time：2019/11/20 16:00:19
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getEmployReportList($params,& $service){
        // error_reporting(E_ALL);
        $pageNum     = $params['pageNum'] ? $params['pageNum'] : 20;
        $currentPage = $params['page'] ? $params['page'] : 1;

        $sql = "SELECT x.productId,sum(x.cnt) as cycnt,x.type,x.proDesc,x.code,count(x.id) as num,e.employName,y.employId
                from sample_chuku2product x
                left join sample_chuku y on y.id=x.chukuId
                left join jichu_employ e on e.id=y.employId
                where 1";
        if($params['productId']){
            $sql .= " and x.productId = '{$params['productId']}'";
        }
        if($params['proDesc']){
            $sql .= " and x.proDesc like '%{$params['proDesc']}%'";
        }
        if($params['code']){
            $sql .= " and x.code like '%{$params['code']}%'";
        }
        if($params['dateFrom']){
            $sql .= " and y.chukuDate >= '{$params['dateFrom']}' and y.chukuDate <= '{$params['dateTo']}'";
        }
        if($params['type']) {
            $sql.=" and x.type = '{$params['type']}'";
        }
        if($params['employId']) {
            $sql.=" and y.employId = '{$params['employId']}'";
        }
        $sql .= " group by x.productId,x.type,y.employId,x.proDesc,x.code order by cycnt desc,num desc";

        // dump($sql);exit;
        FLEA::loadClass('TMIS_Pager');
        $pager = new TMIS_Pager($sql,null,null,$pageNum ,($currentPage - 1));
        $rowset = $pager->findAll();
        // dump($rowset);exit;
        $modelProduct = FLEA::getSingleton('Model_Jichu_Product');
        foreach ($rowset as $key => & $v) {
            //产品明细
            $sql = "select x.*,y.kindName from jichu_product x
                left join jichu_prokind y on x.kindId=y.id
              where x.id = '{$v['productId']}'";
            $tmp = $this->_modelExample->findBySql($sql);
            $v['proCode']  = $tmp[0]['proCode'];
            $v['proName']  = $tmp[0]['proName'];
            $v['kindName'] = $tmp[0]['kindName'];
            //特殊处理下产品数据
            $_tRow = array();
            $modelProduct->filedValue3($_tRow ,$tmp[0] ,$filedsSingle);
            // $v['otherDesc'] = $_tRow;
            foreach ($_tRow as $tk => & $desc) {
                $v['otherDesc'][] = $tk.':'.$desc;
                $v['others'][] = array('title'=>$tk  ,'value'=>$desc);
            }
            $v['othersRows'] = ceil(count($v['others'])/2);//用户计算显示其他信息的行数
            $v['otherDesc'] = join(' ',$v['otherDesc']);
            // $v['otherDesc'] = $modelProduct->filedOtherFormat($tmp[0] ,'text','' ,$filedsDefault);

            //其他数据
            $v['cycnt'] = round($v['cycnt'],6);
        }

        return array('row'=>$rowset,'page'=>$currentPage,'total'=>$pager->totalCount,'pageCount'=>count($rowset));
    }

    /**
     * 客户采样统计
     * Time：2019/11/20 16:00:19
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function getClientReportList($params,& $service){
        // error_reporting(E_ALL);
        $pageNum     = $params['pageNum'] ? $params['pageNum'] : 20;
        $currentPage = $params['page'] ? $params['page'] : 1;

        $sql = "SELECT x.productId,sum(x.cnt) as cycnt,x.type,x.proDesc,x.code,count(x.id) as num,c.compName,y.clientId
                from sample_chuku2product x
                left join sample_chuku y on y.id=x.chukuId
                left join jichu_client c on c.id=y.clientId
                where y.clientId > 0";
        if($params['productId']){
            $sql .= " and x.productId = '{$params['productId']}'";
        }
        if($params['proDesc']){
            $sql .= " and x.proDesc like '%{$params['proDesc']}%'";
        }
        if($params['code']){
            $sql .= " and x.code like '%{$params['code']}%'";
        }
        if($params['dateFrom']){
            $sql .= " and y.chukuDate >= '{$params['dateFrom']}' and y.chukuDate <= '{$params['dateTo']}'";
        }
        if($params['type']) {
            $sql.=" and x.type = '{$params['type']}'";
        }
        if($params['clientId']) {
            $sql.=" and y.clientId = '{$params['clientId']}'";
        }
        $sql .= " group by x.productId,x.type,y.clientId,x.proDesc,x.code order by cycnt desc,num desc";

        // dump($sql);exit;
        FLEA::loadClass('TMIS_Pager');
        $pager = new TMIS_Pager($sql,null,null,$pageNum ,($currentPage - 1));
        $rowset = $pager->findAll();
        // dump($rowset);exit;
        $modelProduct = FLEA::getSingleton('Model_Jichu_Product');
        foreach ($rowset as $key => & $v) {
            //产品明细
            $sql = "select x.*,y.kindName from jichu_product x
                left join jichu_prokind y on x.kindId=y.id
              where x.id = '{$v['productId']}'";
            $tmp = $this->_modelExample->findBySql($sql);
            $v['proCode']  = $tmp[0]['proCode'];
            $v['proName']  = $tmp[0]['proName'];
            $v['kindName'] = $tmp[0]['kindName'];
            //特殊处理下产品数据
            $_tRow = array();
            $modelProduct->filedValue3($_tRow ,$tmp[0] ,$filedsSingle);
            $v['otherDesc'] = array();
            foreach ($_tRow as $tk => & $desc) {
                $v['otherDesc'][] = $tk.':'.$desc;
                $v['others'][] = array('title'=>$tk  ,'value'=>$desc);
            }
            $v['othersRows'] = ceil(count($v['others'])/2)+0;//用户计算显示其他信息的行数
            $v['otherDesc'] = join(' ',$v['otherDesc']);
            // $v['otherDesc'] = $modelProduct->filedOtherFormat($tmp[0] ,'text','' ,$filedsDefault);

            //其他数据
            $v['cycnt'] = round($v['cycnt'],6);
        }

        return array('row'=>$rowset,'page'=>$currentPage,'total'=>$pager->totalCount,'pageCount'=>count($rowset));
    }
}
?>